/*
 * Font.java  0.4.0 / Oct 24, 2010
 *
 * Copyright (c) 2010 - 2011, cute4j.com and contributors.
 *
 * This software is licensed under the LGPL version 2.1 license.
 * A copy of this license is included in the file LICENSE-LGPL.txt.
 */
package com.cute4j.swing.graphics;

import com.cute4j.core.Disposable;
import com.cute4j.swing.Swing;
import com.cute4j.util.CommonConstants;

/**
 * Font Class.
 *
 * @cute4j.native
 *    generate="header"
 *
 * @author Slobodan Vrkacevic
 * @version 0.4.0
 */
public class Font implements Disposable {

// Public Static Fields. -------------------------------------------------------

  /**
   * The plain style constant.
   */
  public static final int PLAIN = 0;

  /**
   * The bold style constant.
   */
  public static final int BOLD = 1;

  /**
   * The italic style constant.
   */
  public static final int ITALIC = 2;

  /**
   * The underline style constant.
   */
  public static final int UNDERLINE = 4;

  /**
   * The overline style constant.
   */
  public static final int OVERLINE = 8;

  /**
   * The strikeout style constant.
   */
  public static final int STRIKEOUT = 16;

// Static Initialization. ------------------------------------------------------

  static {
    Swing.initialize();
  }

// Private Fields. -------------------------------------------------------------

  private long _fontPointer;
  private long _fontMetricsPointer;
  private int fontMetricsReferences;
  private final Object lock;
  private final String name;
  private final int style;
  private final int size;
  private int hashCode; // NOPMD

// Public Constructors. --------------------------------------------------------

  /**
   * Constructs new Font object.
   *
   * @param name the font name
   * @param style the font style
   * @param size the font size
   */
  public Font(String name, int style, int size) {
    lock = new Object();
    if (name == null) {
      this.name = _getDefaultFontName();
    } else {
      this.name = name;
    }
    this.style = style;
    this.size = size;
  }

// Package-Private Constructors. -----------------------------------------------

  Font(String name, int style, int size, long fontPointer) {
    lock = new Object();
    this.name = name;
    this.style = style;
    this.size = size;
    _fontPointer = fontPointer;
  }

// Public Methods. -------------------------------------------------------------

  /**
   * Returns font name.
   *
   * @return font name
   */
  public String getName() {
    return name;
  }

  /**
   * Returns font style.
   *
   * @return font style
   */
  public int getStyle() {
    return style;
  }

  /**
   * Returns font size.
   *
   * @return font size
   */
  public int getSize() {
    return size;
  }

  /**
   * Returns <tt>true</tt> if font is plane.
   *
   * @return <tt>true</tt> if font is plane
   */
  public boolean isPlain() {
    return (style == 0);
  }

  /**
   * Returns <tt>true</tt> if font is bold.
   *
   * @return <tt>true</tt> if font is bold
   */
  public boolean isBold() {
    return ((style & BOLD) != 0);
  }

  /**
   * Returns <tt>true</tt> if font is italic.
   *
   * @return <tt>true</tt> if font is italic
   */
  public boolean isItalic() {
    return ((style & ITALIC) != 0);
  }

  /**
   * Returns <tt>true</tt> if font is underline.
   *
   * @return <tt>true</tt> if font is underline
   */
  public boolean isUnderline() {
    return ((style & UNDERLINE) != 0);
  }

  /**
   * Returns <tt>true</tt> if font is overline.
   *
   * @return <tt>true</tt> if font is overline
   */
  public boolean isOverline() {
    return ((style & OVERLINE) != 0);
  }

  /**
   * Returns <tt>true</tt> if font is overline.
   *
   * @return <tt>true</tt> if font is overline
   */
  public boolean isStrikeOut() {
    return ((style & STRIKEOUT) != 0);
  }

  /**
   * Constructs new Font object using this font and specified style.
   *
   * @param style font style
   *
   * @return derived font
   */
  public Font deriveFont(int style) {
    return new Font(name, style, size);
  }

// Disposable Interface Implementation. ----------------------------------------

  /**
   * {@inheritDoc}
   */
  public final boolean isDisposed() {
    synchronized (lock) {
      return (_fontPointer != 0);
    }
  }

  /**
   * {@inheritDoc}
   */
  public final void dispose() {
    synchronized (lock) {
      if ((_fontPointer != 0) && (fontMetricsReferences == 0)) {
        _destroy(_fontPointer);
        _fontPointer = 0;
      }
    }
  }

// Overridden Methods. ---------------------------------------------------------

  /**
   * {@inheritDoc}
   */
  public boolean equals(Object obj) {
    if (obj == this) {
      return true;
    }
    if ((obj != null) && (obj.getClass() == getClass())) {
      Font font = (Font)obj;
      return (getName().equals(font.getName()) &&
              (getStyle() == font.getStyle()) && (getSize() == font.getSize()));
    }
    return false;
  }

  /**
   * {@inheritDoc}
   */
  public int hashCode() {
    if (hashCode == 0) {
      int hashCode = 3;
      hashCode = 23 * hashCode + getName().hashCode();
      hashCode = 23 * hashCode + getStyle();
      hashCode = 23 * hashCode + getSize();
      this.hashCode = hashCode;
    }
    return hashCode;
  }

  /**
   * {@inheritDoc}
   */
  public String toString() {
    StringBuffer sb = new StringBuffer(getClass().getName());
    sb.append("[name=\"");
    sb.append(getName());
    sb.append("\",style=[");
    if (isPlain()) {
      sb.append("plain");
    } else {
      boolean appendComma = false;
      if (isBold()) {
        sb.append("bold");
        appendComma = true;
      }
      if (isItalic()) {
        if (appendComma) {
          sb.append(CommonConstants.STRING_COMMA);
        }
        sb.append("italic");
        appendComma = true;
      }
      if (isUnderline()) {
        if (appendComma) {
          sb.append(',');
        }
        sb.append("underline");
        appendComma = true;
      }
      if (isOverline()) {
        if (appendComma) {
          sb.append(CommonConstants.STRING_COMMA);
        }
        sb.append("overline");
        appendComma = true;
      }
      if (isStrikeOut()) {
        if (appendComma) {
          sb.append(CommonConstants.STRING_COMMA);
        }
        sb.append("strikeout");
      }
    }
    sb.append("],size=");
    sb.append(getSize());
    sb.append(']');
    return sb.toString();
  }

  /**
   * {@inheritDoc}
   */
  protected final void finalize() {
    synchronized (lock) { // NOPMD
      if (_fontPointer != 0) {
        _destroy(_fontPointer);
        _fontPointer = 0;
      }
    }
  }

// Package-Private Methods. ----------------------------------------------------

  long acquireFontMetricsPointer() {
    synchronized (lock) {
      fontMetricsReferences++;
      if (_fontMetricsPointer == 0) {
        if (_fontPointer == 0) {
          _fontPointer = _create(name, style, size);
          if (_fontPointer == 0) {
            throw new OutOfMemoryError("Font creation failed");
          }
        }
        _fontMetricsPointer = _createFontMetrics(_fontPointer);
        if (_fontMetricsPointer == 0) {
          throw new OutOfMemoryError("FontMetrics creation failed");
        }
      }
      return _fontMetricsPointer;
    }
  }

  void releaseFontMetricsPointer() {
    synchronized (lock) {
      fontMetricsReferences--;
      if ((fontMetricsReferences == 0) && (_fontMetricsPointer != 0)) {
        _destroyFontMetrics(_fontMetricsPointer);
        _fontMetricsPointer = 0;
      }
    }
  }

// Native Callbacks. -----------------------------------------------------------

  long _getFontPointer() { // synchronized by the native code
    if (_fontPointer == 0) {
      _fontPointer = _create(name, style, size);
      if (_fontPointer == 0) {
        throw new OutOfMemoryError("Font creation failed");
      }
    }
    return _fontPointer;
  }

// Package-Private Native Methods. ---------------------------------------------
  
  native long _create(String name, int style, int size);

  native long _createFontMetrics(long fontPonter);

  native void _destroy(long fontPonter);

  native void _destroyFontMetrics(long fontMetricsPointer);

// Package-Private Static Native Methods. --------------------------------------

  static native String _getDefaultFontName();

}
